package com.centling.smartSealForPhone.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.constants.UrlConstants
import com.centling.smartSealForPhone.utils.OkHttpUtil
import com.centling.smartSealForPhone.utils.ShowToast
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_apply_seal_reason.*
import kotlinx.android.synthetic.main.rv_apply_seal_reason_item.view.*
import java.util.*

class AuditorApprovalActivity : TitleBarActivity() {

    private val reasonList = arrayListOf<ApprovalReason>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_apply_seal_reason)

        setTitleBarText("选择审核人")

        rv_apply_seal_reason.layoutManager = GridLayoutManager(mContext, 1)
        rv_apply_seal_reason.adapter = ApplySealReasonAdapter()

        sendRequestToFetchApplySealApproval()
    }

    private fun sendRequestToFetchApplySealApproval() {
        mProcessDialog.setTitle("正在获取审核人").showNormal()
        getEnqueue(String.format(Locale.CHINA, UrlConstants.FETCH_AUDITOR_APPROVAL,
                intent.getStringExtra("which_auditor_apply_seal_id")?:""), object : OkHttpUtil.NetCallBack {
            override fun onSucceed(json: String?) {
                mProcessDialog.dismiss()

                val reasonListBean = Gson().fromJson(json, ReasonListBean::class.java)
                if (reasonListBean?.list != null && !reasonListBean.list!!.isEmpty()) {
                    reasonList.addAll(reasonListBean.list!!)
                } else {
                    ShowToast.shortTime("审核人列表为空")
                    finish()
                }
                rv_apply_seal_reason.adapter.notifyDataSetChanged()
            }

            override fun onFailed(reason: String?) {
                mProcessDialog.dismiss()
                ShowToast.shortTime(reason)
                finish()
            }
        })
    }

    inner class ApplySealReasonAdapter : RecyclerView.Adapter<AuditorApprovalActivity.ReasonViewHolder>() {
        override fun getItemCount(): Int = reasonList.size

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReasonViewHolder = ReasonViewHolder(LayoutInflater.from(mContext).inflate(R.layout.rv_apply_seal_reason_item, parent, false))

        override fun onBindViewHolder(holder: ReasonViewHolder, position: Int) {
            holder.itemView.tv_apply_seal_reason_name.text = reasonList[position].auditorName
            holder.itemView.setOnClickListener {
                /**
                 * 为Intent设置携带返回的值
                 */
                val intent = Intent()
                intent.putExtra("which_auditor_approval_id", reasonList[position].auditorId)
                intent.putExtra("which_auditor_approval_name", reasonList[position].auditorName)
                setResult(Activity.RESULT_OK, intent)
                finish()
            }
        }

    }

    inner class ReasonViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView)

    inner class ReasonListBean {
        var list: List<ApprovalReason>? = null
    }

    data class ApprovalReason(var auditorId: String, val auditorName: String)
}
